package com.cloud.salon.client.business.service.impl;

import com.cloud.salon.client.business.repository.PreferentialRepository;
import com.cloud.salon.client.business.service.PreferentialService;
import com.cloud.salon.common.base.entity.business.Preference;
import com.cloud.salon.common.base.response.Res;
import com.cloud.salon.common.crud.service.impl.CRUDServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 优惠service实现类
 * @Author wolf--LZH
 * @CreateDate 2019/5/17 0017 下午 4:22
 */
@Service
public class PreferentialServiceImpl extends CRUDServiceImpl<Preference, PreferentialRepository> implements PreferentialService {

    @Autowired
    private PreferentialRepository repository;

    public Res<List<Preference>> findByAccount(Integer accountId) {
        List<Preference> preferences = repository.findByAccountId(accountId);

        if (preferences != null)
            return new Res(0, "查找成功", preferences);
        else
            return new Res(-3, "查找失败");
    }

    public Res<List<Preference>> findByAccountAndItem(Preference preference) {
        if (preference.getAccountId() == null || preference.getItemId() == null)
            return new Res(-3, "参数错误");
        List<Preference> preferences = repository.findByAccountIdAndItemId(preference.getAccountId(), preference.getItemId());

        if (preferences != null)
            return new Res(0, "查找成功", preferences);
        else
            return new Res(-3, "查找失败");
    }

    public Res<List<Preference>> findByAccountInId(Integer accountId, List<Integer> preferenceIdList) {
        List<Preference> preferences = new ArrayList<>();

        List<Integer> idList = new ArrayList<>();
        for (Integer id: preferenceIdList) {
            // 若已经包含该id,则先查询已有集合
            if (idList.contains(id)) {
                preferences.addAll(repository.findByAccountIdAndIdIn(accountId, idList));
                idList.clear();
            }
            // 把当前id添加到集合中去
            idList.add(id);
        }
        preferences.addAll(repository.findByAccountIdAndIdIn(accountId, idList));
        if (preferenceIdList.size() != preferences.size())
            return new Res(-3, "查询失败,部分数据不存在");
        return new Res(0, "查找成功", preferences);
    }

    public Res<Integer> setTimes(Integer preferenceId, Integer timesDelta) {
        Integer success = repository.setTimes(preferenceId, timesDelta);

        if (success != null)
            return new Res(0, "修改成功", success);
        else
            return new Res(-3, "修改失败");
    }

    @Override
    public Res<Preference> update(Preference preference) {
        //如果不存在该实体，不准更新数据库，并返回null
        Preference temp = findById(preference.getId()).getData();
        if (temp == null) {
            return new Res(-3, "不存在此实体,请确认后再修改");
        }

        // 保持一下字段不变
        preference.setAccountId(temp.getAccountId());
        preference.setType(temp.getType());
        preference.setItemId(temp.getItemId());
        preference.setMinConsume(temp.getMinConsume());

        return new Res(0, "修改成功", repository.save(preference));
    }
}
